/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.system;

import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.KeyboardFocusManager;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import main.ServicesList;
import model.system.SystemUser;
import utility.com.GradientPanel;
import view.KasirFrame;
import view.OfficeFrame;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
public class LoginFrm extends javax.swing.JFrame {
    public String appCaller;
    /**
     * Creates new form LoginFrm
     */
    public LoginFrm() {
        initComponents();
        initComponentsRules();
         ImageIcon ico = new ImageIcon(getClass().getResource("/Orangagon.png"));
        Image  img = ico.getImage();
        this.setIconImage(img);
        this.setResizable(false);        
        this.setTitle("BTP Office Login");
        GradientPanel pnl = new GradientPanel();
        pnl.setDark(true);
        pnl.setBounds(0, 0, this.getWidth(), this.getHeight()/4);
        pnl.setBackground(Color.white);
        this.add(pnl);
        initForm();
    }
    
    private void initForm(){
        txtPassword.setText("1");
        txtUsername.setText("jay");
        txtUsername.requestFocus();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnLogin = new javax.swing.JButton();
        txtUsername = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        txtUsername.setText("jTextField1");
        txtUsername.setPreferredSize(new java.awt.Dimension(59, 25));

        jLabel1.setText("Username :");

        jLabel2.setText("Password :");

        txtPassword.setText("jPasswordField1");

        btnCancel.setText("Cancel");
        btnCancel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, 0)
                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(160, 160, 160)
                        .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(100, 100, 100)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnLogin)
                    .addComponent(btnCancel))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        
        SystemUser canLogin = ServicesList.getSystemUserService().canLogin(txtUsername.getText(), String.valueOf(txtPassword.getPassword()));
        if (canLogin!=null) {
           if (appCaller.equals("OFFICE")){
               OfficeFrame frm = new OfficeFrame();
               frm.setSelectedUser(canLogin);
               ServicesList.getSystemUserService().setActiveUser(canLogin);
               frm.execute();
           }
           else {
               new KasirFrame().setVisible(true);
           }
            
            this.dispose();
        }
        else {
            JOptionPane.showMessageDialog(this, "Password dan username salah, silahkan periksa kembali", "Warning", JOptionPane.WARNING_MESSAGE);
            txtUsername.requestFocus();
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(this, "Are you sure want to exit?","Confirmation",JOptionPane.YES_NO_OPTION)==0)
            System.exit(1);
    }//GEN-LAST:event_btnCancelActionPerformed

    protected void initComponentsRules() {
        txtUsername.addKeyListener(new ActionKeyListener());
        txtPassword.addKeyListener(new ActionKeyListener());
    }
    
    private void moveNext() {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.focusNextComponent();
    }
     
     class ActionKeyListener implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {}
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode()==KeyEvent.VK_ENTER){
                moveNext();
            }
        }
        @Override
        public void keyReleased(KeyEvent e) {}
       
         
     }
    class ActionFocusGained implements FocusListener {
        @Override public void focusGained(FocusEvent e) { 
            Component componen = (Component) e.getSource(); 
//            if (componen instanceof JTextField) { oldValue = ((JTextField) componen).getText();
//            }else if (componen instanceof JComboBox) { oldValue = ((JComboBox) componen).getSelectedIndex();
//            }else if (componen instanceof JCheckBox) { oldValue = ((JCheckBox) componen).isSelected();
//            }else if (componen instanceof JTextArea) { oldValue = ((JTextArea) componen).getText(); 
//            }else if (componen instanceof JDateChooser) { oldValue = ((JDateChooser) componen).getDate(); }
        }
        
        @Override public void focusLost(FocusEvent e) {
//            Component componen = (Component) e.getSource(); 
//            if (componen instanceof JPasswordField) { newValue = ((JTextField) componen).getText();
//            }else if (componen instanceof JComboBox) { newValue = ((JComboBox) componen).getSelectedIndex();
//            }else if (componen instanceof JCheckBox) { newValue = ((JCheckBox) componen).isSelected();
//            }else if (componen instanceof JTextArea) { newValue = ((JTextArea) componen).getText(); 
//            }else if (componen instanceof JDateChooser) { newValue = ((JDateChooser) componen).getDate(); }
//            
//            if (!newValue.equals(oldValue)) { initButtonState(true); }
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(LoginFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(LoginFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(LoginFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(LoginFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LoginFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
